package com.jq.aution.controller;

import com.jq.aution.model.ProductBidInfo;
import com.jq.aution.model.ProductInfo;
import com.jq.aution.model.UserAccountInfo;
import com.jq.aution.service.ProductBidInfoService;
import com.jq.aution.service.ProductInfoService;
import com.jq.aution.service.UserAccountInfoService;
import com.jq.aution.service.UserInfoService;
import com.jq.aution.service.impl.ProductBidInfoServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 商品操作控制器
 * Created by jiangqi on 2020/09/08
 */
@Controller
@RequestMapping("/product")
public class ProductController {
    private static final Logger LOGGER = LoggerFactory.getLogger(ProductController.class);

    @Autowired
    ProductInfoService productInfoService;
    @Autowired
    UserAccountInfoService userAccountInfoService;
    @Autowired
    UserInfoService userInfoService;
    @Autowired
    ProductBidInfoService productBidInfoService;

    /**
     * 进入竞拍页面
     * @param proId
     * @return
     */
    @RequestMapping("/{proId}")
    public String enterBid(@PathVariable String proId, Model model, HttpSession session){
        //查找该Id对应商品信息
        ProductInfo productInfo;
        UserAccountInfo userAccountInfo;
        long bidTime;
        String userType;
        List<ProductBidInfo> productBidInfoList;
        try{
            productInfo=productInfoService.loadProductById(Integer.valueOf(proId));
            userAccountInfo=userAccountInfoService.loadUserAccountInfoById(productInfo.getOwnerId());
            bidTime=productInfo.getEndTime().getTime();
            Integer id= (Integer) session.getAttribute("userId");
            userType=userInfoService.loadUserById(id).getUserType();
            productBidInfoList=productBidInfoService.loadBidInfoByProductId(Integer.valueOf(proId));
            System.out.println(productBidInfoList.get(0).getBidPrice());
        }catch (Exception e){
            throw e;
        }
        System.out.println(userAccountInfo.getCreditValue());
        model.addAttribute("pi",productInfo);
        model.addAttribute("uai",userAccountInfo);
        model.addAttribute("bidTime",bidTime);
        model.addAttribute("userType",userType);
        model.addAttribute("pbiList",productBidInfoList);
        return "bid";
    }

    /**
     * 出价竞拍
     * @param proId
     * @param bidPrice
     * @param session
     * @return
     */
    @RequestMapping("/{proId}/{bidPrice}")
    public String bid(@PathVariable String proId,@PathVariable String bidPrice,HttpSession session){
        ProductBidInfo productBidInfo;
        ProductInfo productInfo;
        try{
            productBidInfo=new ProductBidInfo();
            Integer bidderId= (Integer) session.getAttribute("userId");
            productBidInfo.setBidderId(bidderId);
            productBidInfo.setBidPrice(Float.valueOf(bidPrice));
            productBidInfo.setProductId(Integer.valueOf(proId));
            if(productBidInfoService.insert(productBidInfo)){
                LOGGER.info("出价成功");
                //如果出价成功，还要更新商品信息
                productInfo=new ProductInfo();
                productInfo.setProductId(Integer.valueOf(proId));
                productInfo.setNowPrice(Float.valueOf(bidPrice));
                if (productInfoService.update(productInfo)){
                    LOGGER.info("更新商品当前价格成功");
                }
            }
        }catch (Exception e){
            throw e;
        }
        return "redirect:/product/{proId}";
    }
}
